import sun.audio.*;
import  java.io.*;

import javax.sound.sampled.*;

public class Sound extends Thread {
//	protected String fileName="footsteps.wav";
	protected String fileName="gunshot.wav";
	protected float setVolume=50.0f;
	
	public Sound(String fileName,float setVolume){
		this.fileName=fileName;
		this.setVolume=setVolume;
	}
        public void run(){
                //String fileName="rocket.wav";
                
                File soundFile = new File( fileName );
                try {
                
                // Create a stream from the given file.
                // Throws IOException or UnsupportedAudioFileException
                AudioInputStream audioInputStream = AudioSystem.getAudioInputStream( soundFile );
                // AudioSystem.getAudioInputStream( inputStream ); // alternate audio stream from inputstream
                        playAudioStream( audioInputStream );
                } catch ( Exception e ) {
                        System.out.println( "Problem with file " + fileName + ":" );
                e.printStackTrace();    
                }
        }        
        
        /** Plays audio from the given audio input stream. */
        
        public void playAudioStream( AudioInputStream audioInputStream ) {
        // Audio format provides information like sample rate, size, channels   
        AudioFormat audioFormat = audioInputStream.getFormat();
       // System.out.println( "Play input audio format=" + audioFormat );

        // Open a data line to play our type of sampled audio.
        
        // Use SourceDataLine for play and TargetDataLine for record.
        
        DataLine.Info info = new DataLine.Info( SourceDataLine.class, audioFormat );
        
        if ( !AudioSystem.isLineSupported( info ) ) {
                System.out.println( "Play.playAudioStream does not handle this type of audio on this system." );
                return;
        }

        try {
        // Create a SourceDataLine for play back (throws LineUnavailableException).
                SourceDataLine dataLine = (SourceDataLine) AudioSystem.getLine( info );
        // System.out.println( "SourceDataLine class=" + dataLine.getClass() );
        // The line acquires system resources (throws LineAvailableException).
        
                dataLine.open( audioFormat );
        
        // Adjust the volume on the output line.        
        if( dataLine.isControlSupported( FloatControl.Type.MASTER_GAIN ) ) {
                FloatControl volume = (FloatControl) dataLine.getControl( FloatControl.Type.MASTER_GAIN );
                volume.setValue( setVolume );
        
        }

        // Allows the line to move data in and out to a port.
        
        dataLine.start();

        // Create a buffer for moving data from the audio stream to the line.
        int bufferSize = (int) audioFormat.getSampleRate() * audioFormat.getFrameSize();
        
        byte [] buffer = new byte[ bufferSize ];
        
        try {
                int bytesRead = 0;
        
                while ( bytesRead >= 0 ) {
                                bytesRead = audioInputStream.read( buffer, 0, buffer.length );
        
                                if ( bytesRead >= 0 ) {
                                
                                int framesWritten = dataLine.write( buffer, 0, bytesRead );
        
                                }
        
                } // while
        
        } catch ( IOException e ) {
        
        e.printStackTrace();
        
        }

        dataLine.drain();       

        
        dataLine.close();
        
        } catch ( LineUnavailableException e ) {
        
        e.printStackTrace();
        
        }
        } // playAudioStream
        
}
